#!/usr/bin/env python
import logging

import unittest
from mock import Mock

from canvaside.canvas.cairo2d import *

class TestColor(unittest.TestCase):
    def setUp(self):
        self.canvas = Mock()
        self.canvas.width = 100
        self.canvas.height = 100

    def testContextState(self):
        s = ContextState()

        self.assertEquals('black', s.fillStyle)
        self.assertEquals('black', s.strokeStyle)

    def testSaveAndRestore(self):
        ctxt = CanvasRenderingContext2D(self.canvas)

        self.assert_(ctxt.state)

        self.assertEquals('black', ctxt.fillStyle)

        ctxt.save()

        ctxt.fillStyle = 'white'

        self.assertEquals('white', ctxt.fillStyle)

        ctxt.restore()

        self.assertEquals('black', ctxt.fillStyle)

    def testGlobalSettings(self):
        ctxt = CanvasRenderingContext2D(self.canvas)

        self.assertEquals(1.0, ctxt.globalAlpha)

        ctxt.globalAlpha = 0.5

        self.assertEquals(0.5, ctxt.globalAlpha)

        ctxt.globalAlpha = 1.5

        self.assertEquals(0.5, ctxt.globalAlpha)

        self.assertEquals('source-over', ctxt.globalCompositeOperation)

        ctxt.globalCompositeOperation = 'copy'

        self.assertEquals('copy', ctxt.globalCompositeOperation)

        ctxt.globalCompositeOperation = 'unknown'

        self.assertEquals('copy', ctxt.globalCompositeOperation)

if __name__=='__main__':
    logging.basicConfig(level=logging.DEBUG if "-v" in sys.argv else logging.WARN,
                        format='%(asctime)s %(levelname)s %(name)s %(message)s')
    unittest.main()